import requests,json
#
def 获取登录认证():
    url= "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
    #应用凭证里的 app id 和 app secret
    post_data = {"app_id": "cli_a55952eac7f9500b", "app_secret": "fPhj9Ju8RPEtvxOcPzDJdbWEWh12vZRQ"}
    r = requests.post(url, data=post_data)
    tat = r.json()["tenant_access_token"]
    print("已获取登录授权")
    return tat

#


def 调用接口(功能,请求数据=None,个性参数=None):

    header = {
    "Content-Type": "application/json; charset=utf-8",
    "Authorization": f"Bearer {获取登录认证()}"}
    url=功能号
    if 功能=="单元格-追加数据":
        url=f"https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/{个性参数[0]}/values_append"
        r2 = requests.post(url, data=json.dumps(请求数据), headers=header)  #请求写入
        print(r2.json()["msg"])  #输出来判断写入是否成功

    elif 功能=="单元格-向单个范围写入数据":
        url=f"https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/{个性参数[0]}/values"
        r2 = requests.post(url, data=json.dumps(请求数据), headers=header)  #请求写入
        print( r2.json()["msg"])  #输出来判断写入是否成功
    elif 功能=="工作表-获取工作表":
        url=f"https://open.feishu.cn/open-apis/sheets/v3/spreadsheets/{个性参数[0]}/sheets/query"
        r2 = requests.get(url, headers=header)
        最大行条数=r2.json()["data"]["sheets"][0]["grid_properties"]["row_count"]
        return  最大行条数

"





#表格新增记录
url="https://open.feishu.cn/open-apis/bitable/v1/apps/BjRmbEUnKaBfx3s2q1YcSKiGnHh/tables/tblMSjy0tgENWKHT/records/batch_create/"
data_raw={
    "records": [
        {
            "fields": {
                "多行文本": "多行文本内容",
                "条码": "qawqe",
                "数字": 100,
                "货币": 3,
                "评分": 3,
                "进度": 0.25,
                "单选": "选项1",
                "多选": ["选项1", "选项2"],
                "日期": 1674206443000,
                "复选框": True,
                "人员": [
                    {
                        "id": "ou_2910013f1e6456f16a0ce75ede950a0a"
                    },
                    {
                        "id": "ou_e04138c9633dd0d2ea166d79f548ab5d"
                    }
                ],
                "群组": [
                    {
                        "id": "oc_cd07f55f14d6f4a4f1b51504e7e97f48"
                    }
                ],
                "电话号码": "18503002413",
                "超链接": {
                    "text": "飞书多维表格官网",
                    "link": "https://www.feishu.cn/product/base"
                },
                "附件": [
                    {
                        "file_token": "Vl3FbVkvnowlgpxpqsAbBrtFcrd"
                    }
                ],
                "单向关联": ["recHTLvO7x", "recbS8zb2m"],
                "双向关联": ["recHTLvO7x", "recbS8zb2m"],
                "地理位置": "116.397755,39.903179"
            }
        }
    ]
}

调用接口("追加数据",url,data_raw)
# #新建多维表格
# data_raw={
#     "name":"一篇新的多维表格"
# }
# url="https://open.feishu.cn/open-apis/bitable/v1/apps"
# 调用接口("追加数据",url,data_raw)

#新增表格数据
# sheetId="011c98"
# data_raw = {
#   "valueRange": {
#     "range": f"{sheetId}!A1:E3",
#     "values": [
#       [
#         "2023/12/25",
#         "收入",
#         "微信",
#         "100",
#         "帐号 老表max"
#       ],
#       [
#         "2023/12/25",
#         "支出",
#         "支付宝",
#         "10",
#         "买东西 老表max"
#       ],
#       [
#         "2023/12/26",
#         "支出",
#         "支付宝",
#         "19.9",
#         "买东西 老表max"
#       ],
#     ]
#   }
# }
# url="https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/XkIas3zWxhjxm7twgyicyxCinQb/values_append"
# 调用接口("追加数据",url,data_raw)
